package portlet;

import java.io.IOException;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.portlet.PortletRequest;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;

import oracle.binding.OperationBinding;

import oracle.jbo.Row;
import oracle.jbo.RowIterator;
import oracle.jbo.ViewObject;
import oracle.jbo.domain.Number;

public class Utils {
    public Utils() {
        super();
    }

    public static Locale getPortalLocale() {
        Locale portalLocale = new Locale("vi", "VN");
        try {
            portalLocale =
                    new Locale(getPortletRequest().getParameter("lang"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return portalLocale;
    }

    public static String getResource(String resourceKey) {
        Locale locale = getPortalLocale();
        ResourceBundle bundle =
            ResourceBundle.getBundle("portlet.resource.RSSBundle",
                                     locale);
        return bundle.getString(resourceKey);
    }

    public static PortletRequest getPortletRequest() {
        PortletRequest req =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return req;
    }

    public static void redirect(String url) {
        ExternalContext externalContext =
            FacesContext.getCurrentInstance().getExternalContext();
        try {
            externalContext.redirect(url);
        } catch (IOException e) {
        }
    }

    public static oracle.jbo.domain.Date getCurrentDate() {
        Date now = new Date();
        Timestamp timestamp = new Timestamp(now.getTime());
        oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(timestamp);
        return jboDate;
    }

    public static enum ENUM_CAU_HINH_KHOA {
        FILE_ROOT("FILE_ROOT"),
        RSS_DIR("RSS_DIR");
        String khoa;

        private ENUM_CAU_HINH_KHOA(String khoa) {
            this.khoa = khoa;
        }

        public String getValue(String iteratorName) {
            DCIteratorBinding it = Utils.getIteratorBinding(iteratorName);
            it.setRangeSize(-1);
            it.executeQuery();
            RowIterator rowIterator =
                it.findRowsByAttributeValues(new String[] { "Khoa" },
                                             new Object[] { khoa });
            if (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                return (String)row.getAttribute("GiaTri");
            }
            return null;
        }

        private String khoaHt = "/c:/root";

        public String KHOA_HT() {
            return khoaHt;
        }

        private String khoaRssFile = "rss";

        public String KHOA_RSS_FILE() {
            return khoaRssFile;
        }

        public String getKhoa() {
            return khoa;
        }
    }

    public static enum ENUM_MA_UNG_DUNG {
        APTC(1),
        PAKN(2),
        TTPH(3),
        DLCT(4),
        TTHC(5),
        DTVB(6),
        KNTC(7),
        RSS(8);
        Number maUngDung;

        private ENUM_MA_UNG_DUNG(int maUngDung) {
            this.maUngDung = new Number(maUngDung);
        }

        public void setMaUngDung(Number maUngDung) {
            this.maUngDung = maUngDung;
        }

        public Number getMaUngDung() {
            return maUngDung;
        }
    }

    public static List execute(String operator) {
        try {
            BindingContainer bindings = getBindings();
            OperationBinding operationBinding =
                bindings.getOperationBinding(operator);
            Object result = operationBinding.execute();
            if (operationBinding.getErrors() != null &&
                operationBinding.getErrors().size() > 0) {
                return operationBinding.getErrors();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public static DCIteratorBinding getIteratorBinding(String iteratorName) {
        return getDCBindingContainer().findIteratorBinding(iteratorName);
    }

    public static DCBindingContainer getDCBindingContainer() {
        return (DCBindingContainer)getBindingContainer();
    }

    public static BindingContainer getBindingContainer() {
        return (BindingContainer)getElExpression("#{bindings}");
    }

    /**
     * @param el
     * @return
     */
    public static Object getElExpression(String el) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =
            facesContext.getApplication().getExpressionFactory();
        ValueExpression valueExp =
            expressionFactory.createValueExpression(elContext, el,
                                                    Object.class);
        return valueExp.getValue(elContext);
    }

    /**
     * @param el
     * @param val
     */
    public static void setElExpression(String el, Object val) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =
            facesContext.getApplication().getExpressionFactory();
        ValueExpression exp =
            expressionFactory.createValueExpression(elContext, el,
                                                    Object.class);
        exp.setValue(elContext, val);
    }

    /**
     * Refresh a View Iterator
     * @param iteratorName
     */
    public static void iteratorRefresh(String iteratorName) {
        try {
            DCBindingContainer dcBinding =
                (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
            DCIteratorBinding ViewIterator =
                (DCIteratorBinding)dcBinding.get(iteratorName);
            ViewIterator.getViewObject().clearCache();
            int oldMode = ViewIterator.getViewObject().getQueryMode();
            ViewIterator.getViewObject().setQueryMode(ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);
            ViewIterator.executeQuery();
            ViewIterator.getViewObject().setQueryMode(oldMode);
            ViewIterator.refresh(DCIteratorBinding.RANGESIZE_UNLIMITED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
